package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

import DAO.BaseDAO;
import DTO.Lb;
import pageUtil.PageB;
import service.PageService;
import serviceImpl.LbDAO;
import util.UtilMD5;

@WebServlet(name = "lb", urlPatterns = "/lb")
public class LbServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public LbServlet() {

	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		service(request, response);
	}

	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		System.out.println("进入了类别处理Servlet中");
		String flag = null;
		flag = request.getParameter("flag");
		switch (flag) {
		case "findall":
			try {
				find_all(request, response);
			} catch (Exception e) {
				e.printStackTrace();
			}
			break;
		case "delete":
			try {
				delete(request, response);
			} catch (Exception e) {
				e.printStackTrace();
			}
			break;
		case "add":
			try {
				add(request, response);
			} catch (Exception e) {
				e.printStackTrace();
			}
			break;
		case "update":
			try {
				update(request, response);
			} catch (Exception e) {
				e.printStackTrace();
			}
			break;
		case "getData":
			try {
				getData(request, response);
			} catch (Exception e) {
				e.printStackTrace();
			}
			break;
			
		}
	}
	
	protected void getData(HttpServletRequest request, HttpServletResponse response) throws Exception {
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html; charset=UTF-8");
        request.setCharacterEncoding("utf-8");
        try {
        	 String id = request.getParameter("id");
             BaseDAO<Lb> lbDAO = new LbDAO();
             Lb lb = new Lb();
             lb = lbDAO.findById(id);
             request.setAttribute("lb", lb);
             request.getRequestDispatcher("add_lb.jsp").forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(e.getMessage());
		}
	}
	
	protected void update(HttpServletRequest request, HttpServletResponse response) throws Exception {
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html; charset=UTF-8");
        request.setCharacterEncoding("utf-8");
        String lbname = new String(request.getParameter("lbname").getBytes("ISO8859-1"),"UTF-8");
        String id = request.getParameter("id");
        String lbcs = request.getParameter("lbcs");
        String sql = "update lb set lbname =?,lbcs=? where id=" + id;
        Lb lb = new Lb();
        lb.setLbname(lbname);
        lb.setLbcs(lbcs);
        BaseDAO<Lb> lbDAO = new LbDAO();
        if(Integer.parseInt((lbDAO.findByPropert(lb).getLbcs()))>=1) {
    		request.setAttribute("msg","参数重复");
    		request.getRequestDispatcher("lb_list.jsp").forward(request, response);
    		return;
    	}
        boolean boo = lbDAO.update(lb, sql);
        if(boo) {
        	request.setAttribute("msg","修改成功");
    		request.getRequestDispatcher("lb_list.jsp").forward(request, response);
        }else {
        	request.setAttribute("msg","修改失败");
    		request.getRequestDispatcher("lb_list.jsp").forward(request, response);
        }
	}

	protected void add(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String sql = "insert into lb(lbcs,lbname,cjsj) values(?,?,?)";
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html; charset=UTF-8");
        request.setCharacterEncoding("utf-8");
        String lbname = new String(request.getParameter("lbname").getBytes("ISO8859-1"),"UTF-8");
        String lbcs = request.getParameter("lbcs");
        String cjsj = UtilMD5.dateToString(new Date());
        Lb lb = new Lb();
        lb.setLbname(lbname);
        lb.setCjsj(cjsj);
        lb.setLbcs(lbcs);
        try {
        	BaseDAO<Lb> lbDAO = new LbDAO();
        	if(Integer.parseInt((lbDAO.findByPropert(lb).getLbcs()))>=1) {
        		request.setAttribute("msg","参数重复");
        		request.getRequestDispatcher("lb_list.jsp").forward(request, response);
        		return;
        	}
        	boolean boo = lbDAO.add(lb, sql);
        	if(boo) {
        		request.setAttribute("msg","新增成功");
        		request.getRequestDispatcher("lb_list.jsp").forward(request, response);
        	}else {
        		request.setAttribute("msg","新增失败");
        		request.getRequestDispatcher("lb_list.jsp").forward(request, response);
        	}
		} catch (Exception e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
	}
	
	protected void find_all(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String sql = "select id,lbname,lbcs,cjsj from lb limit ?,?";
		String sql2 = "select count(id) from lb";
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		String currentPage = request.getParameter("cp");
		currentPage = currentPage == null ? "1" : currentPage;
		PageB<Lb> pageBean = new PageB<Lb>();
		pageBean.setCurrentPage(Integer.parseInt(currentPage));
		pageBean.setPageSize(5);
		PageService<Lb> pageservice = new PageService<Lb>(new LbDAO());
		PageB<Lb> bean = pageservice.paging(pageBean, sql, sql2);
		String page = JSON.toJSONString(bean);
		PrintWriter out = response.getWriter();
		out.print(page);
		out.flush();
	}

	protected void delete(HttpServletRequest request, HttpServletResponse response) throws Exception {
		try {
			response.encodeRedirectURL("utf-8");
			response.setCharacterEncoding("utf-8");
			response.setContentType("text/html; charset=UTF-8");
			request.setCharacterEncoding("utf-8");
			String ID = request.getParameter("id");
			Lb lb = new Lb();
			lb.setId(Integer.parseInt(ID));
			BaseDAO<Lb> ed = new LbDAO();
			if (ed.delete(lb)) {
				request.setAttribute("msg", "删除成功");
				request.getRequestDispatcher("lb_list.jsp").forward(request, response);
			} else {
				request.setAttribute("msg", "删除失败");
				request.getRequestDispatcher("lb_list.jsp").forward(request, response);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
